from django.db import models
from django.contrib import admin

import codecs

class ScraperScript(models.Model):
    dirname     = models.CharField(max_length=200)   # readers|detectors
    filename    = models.CharField(max_length=200)
    last_edit   = models.DateTimeField(blank=True, null=True)
    last_run    = models.DateTimeField(blank=True, null=True)
    
    #modulename  = models.CharField(max_length=200) # same as filename, but without the .py
    
    class Meta:
        ordering = ('dirname','filename')  
    
    def __unicode__(self):
        return "%s/%s" % (self.dirname, self.filename)


# Change to Reading
class ScrapedText(models.Model):
    scraper_tag = models.CharField(max_length=200)   # change to tag
    name        = models.CharField(max_length=200)
    url         = models.TextField()
    scrape_time = models.DateTimeField(blank=True, null=True)   # change to timescraped
    text        = models.TextField()
    
    submitter   = models.ForeignKey('ScopeUser', null=True)
    
    fileext     = models.CharField(max_length=20, blank=True)   # actual file type (mimetype)
    diff        = models.TextField()  # istead of corrected text
    
    #filelength = IntField
    
    def __unicode__(self):
        return "%s:%s" % (self.name, self.url[:50])
    
    def gettext(self):
        return self.text  # to apply the diff

    # copy save and load reading into here and get the text left on the disk


# these really belong in the scope application, not the codewiki application
class ScopeUser(models.Model):
    name        = models.CharField(max_length=100)
    email       = models.CharField(max_length=100, unique=True)
    password    = models.CharField(max_length=100)
    lat         = models.FloatField(blank=True, null=True)
    lon         = models.FloatField(blank=True, null=True)
    district    = models.CharField(max_length=100, blank=True, null=True)
    postcode    = models.CharField(max_length=20, blank=True, null=True)

    def __unicode__(self):
        return self.name


class ScopeEvent(models.Model):
    title       = models.CharField(max_length=100)
    summary     = models.TextField(blank=True, null=True)
    url         = models.CharField(max_length=200)
    source      = models.CharField(max_length=100)
    refid       = models.CharField(max_length=100)
    quantity    = models.FloatField(blank=True, null=True)
    
    evt_time    = models.DateTimeField(blank=True, null=True)

    postcode    = models.CharField(max_length=20, blank=True, null=True)
    lat         = models.FloatField(blank=True, null=True)
    lon         = models.FloatField(blank=True, null=True)
    radius      = models.FloatField(blank=True, null=True)
    district    = models.CharField(max_length=100, blank=True, null=True)

    related_evt = models.ForeignKey("ScopeEvent", blank=True, null=True)

    submitter   = models.ForeignKey(ScopeUser)
    sub_time    = models.DateTimeField()

    def __unicode__(self):
        return '<a href="' + self.url + '">' + self.title + '</a> ' + str((self.district,self.lat,self.lon,self.postcode,self.evt_time))



class ScopeMessage(models.Model):
    user        = models.ForeignKey(ScopeUser)
    event       = models.ForeignKey(ScopeEvent)
    time        = models.DateTimeField()

    def __unicode__(self):
        return self.time
    



